package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.dto.Dto;
import cn.itrip.utlis.DtoUtils;
import cn.itrip.vo.ItripTokenVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 提供给前端的验证token接口
 */
@Controller
@RequestMapping("/api")
public class TokenController {

    @Resource
    private TokenService tokenService;

    /**
     * 用于判断用户token的合法性，及置换
     * @param request
     * @return
     */
    @ApiOperation(value = "token验证置换",httpMethod = "post")
    @RequestMapping(value = "/retoken",method = RequestMethod.POST)
    @ResponseBody
    public Dto replacement(HttpServletRequest request){

        ItripTokenVO itripTokenVO = null;

        try {
            itripTokenVO = tokenService.verifyToken(request.getHeader("user-agent"),request.getHeader("token"));
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail(e.getMessage(),"447");
        }

        return DtoUtils.returnDataSuccess(itripTokenVO);
    }

}
